package nju.mscs.gui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.SwingUtilities;

import nju.mscs.businesslogic.GOG;
import nju.mscs.businesslogic.GetOutGift;
import nju.mscs.businesslogic.SC;
import nju.mscs.businesslogic.SearchController;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Gifts extends javax.swing.JFrame {
	private JLabel jLabel2;
	private JButton jButton2;
	private JButton jButton3;
	private JTable jTable2;
	private JTable jTable1;
	private JLabel jLabel3;
	private JLabel jLabel1;
	private JButton jButton1;
    private DefaultTableModel jTable1Model;
    private DefaultTableModel jTable2Model;
    private int selectrow;
    private GetOutGift gog = new GOG();
    private SearchController sc = new SC();
	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {

			public void run() {
				Gifts inst = new Gifts();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
				
			}
		});
	}
	
	public Gifts() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setUndecorated(true);
			
			{
				jLabel2 = new JLabel();
				getContentPane().add(jLabel2);
				jLabel2.setText("\u793c\u54c1\u8d60\u9001");
				jLabel2.setBounds(67, 18, 175, 45);
				jLabel2.setFont(new java.awt.Font("微软雅黑",1,36));
			}
			{
				jButton1 = new JButton();
				getContentPane().add(jButton1);
				jButton1.setText("\u4fdd\u5b58");
				jButton1.setBounds(771, 659, 113, 63);
				jButton1.setFont(new java.awt.Font("微软雅黑",0,28));
				jButton1.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						jButton1ActionPerformed(evt);
					}
				});
			}
			{
				jButton2 = new JButton();
				getContentPane().add(jButton2);
				jButton2.setText("\u8fd4\u56de");
				jButton2.setBounds(1045, 659, 98, 63);
				jButton2.setFont(new java.awt.Font("微软雅黑",0,28));
			}
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setText("\u4f1a\u5458\u5217\u8868");
				jLabel1.setBounds(67, 75, 117, 35);
				jLabel1.setFont(new java.awt.Font("微软雅黑",0,28));
			}
			{
				jLabel3 = new JLabel();
				getContentPane().add(jLabel3);
				jLabel3.setText("\u793c\u54c1\u5217\u8868");
				jLabel3.setBounds(654, 57, 117, 50);
				jLabel3.setFont(new java.awt.Font("微软雅黑",0,28));
			}
			{
				jTable1Model = 
					new DefaultTableModel(
							new String[][]{ },
							new String[] {"会员编号","会员名称","赠送原因","礼物编号","礼品名称"});
				jTable1 = new JTable(jTable1Model){
					public void changeSelection(int rowIndex, int columnIndex,
			                boolean toggle, boolean extend){
			                super.changeSelection(rowIndex, columnIndex, toggle, extend);
			                super.editCellAt(rowIndex, columnIndex, null);}
		             public boolean isCellEditable(int row, int column) 
	                  {          if((column==0)||(column==1)){
	                             return false;}
	                  return true;
	                  }//表格不允许被编辑
	                  };		
	                  
				getContentPane().add(jTable1);
				JScrollPane scrollpane1 = new JScrollPane(jTable1);
				getContentPane().add(scrollpane1);
				scrollpane1.setBounds(38, 133, 496, 481);
				jTable1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
				jTable1.setSelectionBackground(Color.BLACK);
				jTable1.setSelectionForeground(Color.WHITE);
				jTable1.getTableHeader().setFont(new java.awt.Font("微软雅黑",0,28));
				jTable1.setFont(new java.awt.Font("微软雅黑",0,28));
				jTable1.setRowHeight(30);
				jTable1.getSelectionModel().addListSelectionListener(new ListSelectionListener(){    
		            public void valueChanged(ListSelectionEvent e)  
		            {  
		                selectrow = jTable1.getSelectedRow();  
		            }  
		        });  
				/*jTable1.getModel().addTableModelListener(new TableModelListener()  
		        {  
		         
		            public void tableChanged(TableModelEvent e)  
		            {  
		               selectrow = e.getFirstRow();
		  
		            }  
		        });  */
				int rowcount1=gog.showList().length;
				for(int rownum1=0;rownum1<rowcount1;rownum1++)
			    jTable1Model.addRow(gog.showList()[rownum1]);
				
				
				/*for(int i=0;i<jTable1.getRowCount();i++){
					if(jTable1.isRowSelected(i)){
						String giftid = jTable2.getValueAt(jTable2.getSelectedRow(),0).toString();
						jTable1.setValueAt(giftid, i, 1);
					}
				}*/
			}
			{
				jTable2Model = 
					new DefaultTableModel(
							new String[][]{ },
							new String[] {"礼品编号","礼物名称"});
				jTable2 = new JTable(jTable2Model){
		             public boolean isCellEditable(int row, int column) 
	                  {          if((column==0)||(column==1)){
	                             return false;}
	                  return true;
	                  }//表格不允许被编辑
	                  };				
				getContentPane().add(jTable2);
				JScrollPane scrollpane2 = new JScrollPane(jTable2);
				getContentPane().add(scrollpane2);
				scrollpane2.setBounds(654, 125, 625, 489);
				jTable2.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
				jTable2.setSelectionBackground(Color.BLACK);
				jTable2.setSelectionForeground(Color.WHITE);
				jTable2.getTableHeader().setFont(new java.awt.Font("微软雅黑",0,28));
				jTable2.setFont(new java.awt.Font("微软雅黑",0,28));
				jTable2.setRowHeight(30);
				jTable2.getSelectionModel().addListSelectionListener(new ListSelectionListener(){    
		            public void valueChanged(ListSelectionEvent e)  
		            {  
		            	int row = jTable2.getSelectedRow();
		                String giftid = jTable2.getValueAt(row, 0).toString();
		                String giftname = jTable2.getValueAt(row, 1).toString();
		                jTable1.setValueAt(giftid, selectrow, 3);
		                jTable1.setValueAt(giftname, selectrow, 4);
		                
		            }  
		        });  
				/*jTable2.getModel().addTableModelListener(new TableModelListener()  
		        {  		         
		            public void tableChanged(TableModelEvent e)  
		            {  
		               int row = e.getFirstRow();
		               String giftid = jTable2.getValueAt(row, 0).toString();
		               jTable1.setValueAt(giftid, selectrow, 1);
		            }  
		        });  */
				
				int rowcount2=sc.getGiftList().length;
				for(int rownum2=0;rownum2<rowcount2;rownum2++)
			    jTable2Model.addRow(sc.getGiftList()[rownum2]);
				String[] test2 = new String[]{"2","奥利奥威化"};
				jTable2Model.addRow(test2);
			}
			{
				jButton3 = new JButton();
				getContentPane().add(jButton3);
				jButton3.setText("\u53d6\u6d88\u8d60\u9001");
				jButton3.setBounds(165, 660, 160, 62);
				jButton3.setFont(new java.awt.Font("微软雅黑",0,28));
				jButton3.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						jButton3ActionPerformed(evt);
					}
				});
			}
			pack();
			this.setSize(1328, 848);
			getGraphicsConfiguration().getDevice().setFullScreenWindow(this);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	private void jButton3ActionPerformed(ActionEvent evt) {
		//System.out.println("jButton3.actionPerformed, event="+evt);
		jTable1.setValueAt(null, selectrow, 2);
		jTable1.setValueAt(null, selectrow, 3);
		jTable1.setValueAt(null, selectrow, 4);
	}//取消赠送按钮
	
	private void jButton1ActionPerformed(ActionEvent evt) {
		//System.out.println("jButton1.actionPerformed, event="+evt);
		int rownum = jTable1.getRowCount();
		String[][] giftinfo = new String[rownum][2];
		for(int i=0;i<rownum;i++){
			for(int j=0;j<3;j=j+3){
				giftinfo[i][j]=jTable1.getValueAt(i, j).toString();
			}
		}
		Boolean giftresult = gog.storeGG(giftinfo);
		if(giftresult){
			JOptionPane.showMessageDialog(this,"保存成功！");
			return;
		}
		else{
			JOptionPane.showMessageDialog(this,"保存失败！");
			return;
		}	
	}//保存按钮

}
